<?php
namespace  common\components\services\goods;

use common\components\helpers\Fun;
use doumiaoduo\buildform\BuildForm;
use frontend\models\datas\ClassManage;
use frontend\models\datas\Goods;
use frontend\models\datas\GoodsAttr;
use frontend\models\datas\Mechanic;
use yii\base\Action;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;

class SetDistributionAction extends Action{

    public $type;

    public function run(){
        $this->controller->layout=false;
        if (\Yii::$app->request->get('type_id') == null || \Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('frontend', '错误请求'));
        }
        $this->type= \Yii::$app->request->get('type_id');
        $model = Goods::findOne(\Yii::$app->request->get('id'));
        $attr = Fun::getGoodsAttr($model->id);
        $model->scenario = 'set';
        if ($model ==null) {
            throw new NotFoundHttpException(\Yii::t('frontend', '信息不存在'));
        }
        $form = new BuildForm('@app/components/forms/goodsgroupForm.php');//直接使用组合商品的form，所以命名没更改
        $form->loadModel(['goods' => $model]);
        $form->loadDefaultValues();
        if ($form->loadData(\Yii::$app->request->post())&& $form->validate()) {
            $model->is_shelve = 2;
            if ($model->save(false)) {
                return $this->controller->redirectMessage(\Yii::t('frontend', '信息修改成功'), Fun::url([\Yii::$app->controller->id.'/index','type_id' => $this->type]));
            }
        }
        return $this->controller->render('@common/components/services/goods/set.php',['form' => $form->render(),'attr'=>$attr]);
    }

}